<?php

namespace app\api\controller;

use app\admin\model\Staff;
use app\common\controller\Api;
use app\common\library\Email;
use app\common\model\Order as OrderModel;
use app\common\model\Staff as StaffModel;
use app\common\model\StoreProject;
use app\common\model\Suborder;
use think\Db;
use think\Exception;
use think\exception\HttpResponseException;
use think\Request;

class Order extends Api
{

    protected $noNeedRight = ['*'];

    //订单列表
    public function lists()
    {
        /*$store_id = input('store_id','');
        if (empty($store_id)){
            $this->error('缺少参数门店id');
        }*/
        $search_type = input('search_type',0);

        $keyword = input('keyword','');

        //$where = ['o.store_id'=>$store_id];
        $where = [];
        $date = input('date','');

        if ($date){
            $start_date = empty($date) ? strtotime(date('Y-m', strtotime("today"))) : strtotime($date);
            $end_date = strtotime("+1 month", $start_date);
            $where['o.date'] = ['between', [date('Y-m-d H:i:s',$start_date), date('Y-m-d H:i:s',$end_date)]];
        }

        if ($keyword){
            if ($search_type==0){
                //订单搜索
                $where['o.last_name|o.first_name|o.order_no|o.phone|o.email'] = ['like',"%$keyword%"];
            } elseif ($search_type==1){
                //项目搜索
                $where['sp.project_name'] = ['like',"%$keyword%"];
            } elseif ($search_type==2){
                //门店搜索
                $where['s.store_name'] = ['like',"%$keyword%"];
            } elseif ($search_type==3){
                //技师搜索
                $where['su.nickname'] = ['like',"%$keyword%"];
            }
        }

        $order_model = new OrderModel();

        $list = $order_model->lists($where);

        $this->success('',$list);
    }

    //订单详情
    public function info()
    {
        $order_id = input('order_id',0);

        $info = OrderModel::get($order_id);
        if (!$info)$this->error('订单不存在');
        $order_model = new OrderModel();

        $res = $order_model->info($order_id);

        $this->success('',$res);
    }


    //分配员工
    public function allot()
    {
        $order_id = input('order_id',0);
        $staff_id = input('staff_id',0);

        $info = OrderModel::get($order_id);
        if (!$info)$this->error('订单不存在');
        $info->staff_id = $staff_id;
        $info->allot_time = time();

        if ($info->save()){
            $staff_model = new \app\common\model\User();
            $staff_model->edit($staff_id,3);
            $this->success('分配成功');
        } else {
            $this->error('分配失败');
        }

    }

    //根据日期查订单列表视图
    public function date()
    {
        $store_id = input('store_id','');
        if (empty($store_id)){
            $this->error('缺少参数门店id');
        }
        $date = input('date','');
        if (!$date)$this->error('缺少参数日期');
        $keyword = input('keyword','');

        $where = ['o.store_id'=>$store_id,'o.is_end'=>0];
        if ($keyword){
            $where['o.last_name|o.first_name'] = ['like',"%$keyword%"];
        }
        $order_model = new OrderModel();
        $list = $order_model->dateorder($where,$date);

        $this->success('',$list);
    }

    //根据日期查订单列表日历视图
    public function datetime()
    {
        $store_id = input('store_id','');
        if (empty($store_id)){
            $this->error('缺少参数门店id');
        }
        $date = input('date','');
        if (!$date)$this->error('缺少参数日期');

        $where = ['o.store_id'=>$store_id,'o.is_end'=>0];
        $order_model = new OrderModel();

        $list = $order_model->datetimeorder($where,$date);

        $this->success('',$list);
    }

    //取消订单
    public function cancel()
    {
        try {
            $order_id = input('order_id',0);
            $order_model = new OrderModel();

            $res = $order_model->cancel($order_id);

        }  catch (Exception $e) {
            $this->error($e->getMessage());
        }

        $this->success('取消成功');
    }

    //修改订单
    public function modify()
    {
        $order_id = input('order_id',0);
        $store_id = input('store_id',0);
        $project_id = input('project_id',0);
        $num = input('num',0);
        $date = input('date','');
        $info = OrderModel::get($order_id);
        if (!$info)$this->error('订单不存在');
        if (!$store_id)$this->error('请选择门店');
        if (!$project_id)$this->error('请选择服务项目');
        if (!$num)$this->error('请选择服务项目数量');
        if (!$date)$this->error('请选择服务时间');
        $info->store_id = $store_id;
        $info->project_id = $project_id;
        $info->num = $num;
        $info->date = $date;
        $info->edit_type = 0;
        $info->is_edit = 1;
        $info->edit_time = time();
        $info->date_timestamp = strtotime($date);
        $info->save();
        $this->success('修改成功');
    }

    //确认订单
    public function confirm()
    {
        $order_id = input('order_id',0);

        $info = OrderModel::get($order_id);
        if (!$info)$this->error('订单不存在');
        if ($info->is_confirm == 1)$this->error('订单已确认');
        $store = \app\common\model\Store::get($info->store_id);
        $project = StoreProject::get($info->project_id);
        Db::startTrans();
        try {
            $info->is_confirm = 1;
            $info->confirm_time = time();
            $info->save();

            $message_title = \app\common\model\Config::get(['name'=>'confirm_title'])->value;
            $confirm_content = \app\common\model\Config::get(['name'=>'confirm_content'])->value;
            //$message_title = '您预约'.$store['store_name'].'门店的项目:'.$project['project_name'].'已确认';
            $request = Request::instance();
            $domain=$request->domain();
            $message_content = $confirm_content.'订单链接: '.$domain.'/dist/#/complete?order_id='.$order_id;
            $email = new Email;
            $result = $email
                ->to($info->email)
                ->subject($message_title)
                ->message($message_content)
                ->send();
            //dump($email->getError());die;
            if (!$result) {
                Db::rollback();
                throw new Exception($email->getError());
                //$this->error($email->getError());
            }
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            $this->error($e->getMessage());
        }
        $this->success('确认成功');
    }

    //客户信息列表
    public function client()
    {
        $store_id = input('store_id','');
        if (empty($store_id)){
            $this->error('缺少参数门店id');
        }
        $date = input('date');


        $where = ['o.store_id'=>$store_id];
        if ($date){
            $start_date = empty($date) ? strtotime(date('Y-m', strtotime("today"))) : strtotime($date);
            $end_date = strtotime("+1 month", $start_date);
            $where['o.date'] = ['between', [date('Y-m-d H:i:s',$start_date), date('Y-m-d H:i:s',$end_date)]];
        }

        $list = OrderModel::alias('o')
            ->where($where)
            ->field('o.order_id,o.last_name,o.first_name,o.phone,o.email,o.date')
            ->order('o.createtime desc')
            ->paginate(10, false, ['query' => request()->get()]);

        $this->success('',$list);

    }


    //支付
    public function pay()
    {
        $order_id = input('order_id',0);
        $cash_price = input('cash_price',0);
        $gift_card_tip_price = input('gift_card_tip_price',0);
        $swiping_card_price = input('swiping_card_price',0);
        $swiping_card_tip_price = input('swiping_card_tip_price',0);
        $gift_card_price = input('gift_card_price',0);

        $info = OrderModel::get($order_id);
        if (!$info)$this->error('订单不存在');

        $order_model = new OrderModel();

        $res = $order_model->pay($order_id,$cash_price,$gift_card_tip_price,$swiping_card_price,$swiping_card_tip_price,$gift_card_price);

        $this->success('支付成功',$res);
    }

    //订单支付的项目金额/数量
    public function paydata()
    {
        $order_id = input('order_id',0);

        $order_model = new OrderModel();

        $info = $order_model->paydata($order_id);

        $this->success('',$info);
    }


    //快捷开单
    public function fastOpen()
    {
        $param = input();

        $order_model = new OrderModel();

        $list = $order_model->fastOpenOrder($param);

        $this->success('',$list);
    }

}